<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 10/4/14
 * Time: 8:39 PM
 */

namespace Backend\Modules\Video\Controllers;

use Common\library\Common;
use Library\Constant\Constant;
use \Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Models\Video;
use Backend\Modules\Video\Forms\FormSeachVideo;

class AjaxController extends \BackendController
{
    public function loadVideoAction($video_group_id)
    {
        $this->view->setTemplateAfter('empty');

        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        $artistType = $this->request->get('type') ? $this->request->get('type') : 1;

        $videos = Video::find();

        $formSearchVideo = new FormSeachVideo();

        if ($currentPage == 0) {
            $currentPage = 1;
        }

        $pager = new Pager(
            new Paginator(array(
                'data'  => $videos,
                'limit' => Constant::ITEM_PER_PAGE_20,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                'rangeLength' => 5,
                'urlMask'     => 'http://localhost/javhd/javhd-admin/video/artist?page={%page_number}',
            )
        );
        $this->view->pager = $pager;
        $this->view->video_group_id = $video_group_id;
        $this->view->formSearchVideo = $formSearchVideo;
        $this->view->render("ajax", "modal/modal_video");
    }

    public function seachVideoAction()
    {
        if($this->request->isAjax())
        {
            if($this->request->hasPost('name'))
            {

            }
            $this->view->setTemplateAfter('empty');

            $currentPage = abs($this->request->getQuery('page', 'int', 1));
            $artistType = $this->request->get('type') ? $this->request->get('type') : 1;

            $videos = Video::find();

            $formSearchVideo = new FormSeachVideo();

            if ($currentPage == 0) {
                $currentPage = 1;
            }

            $pager = new Pager(
                new Paginator(array(
                    'data'  => $videos,
                    'limit' => Constant::ITEM_PER_PAGE_20,
                    'page'  => $currentPage,
                )),
                array(
                    'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                    'rangeLength' => 5,
                    'urlMask'     => 'http://localhost/javhd/javhd-admin/video/artist?page={%page_number}',
                )
            );
            $this->view->pager = $pager;
            $this->view->render("ajax", "result_search_video");

            $json['success'] = true;
            $json['html'] = $this->view->getContent();
            $this->response->setContent(json_encode($json));
            //Return the response
            return $this->response->send();
        }
    }
} 